<?php
namespace Login\Model;
use Think\Model;
class AdminModel extends Model {
	public $table='think_admin';
	public $page; //当前页
	public $perpage; //每页显示
	public $admin_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词

	//判断是否管理员
	public function check_admin()
	{
		$Model=M("Admin");
		$condition['admin_name']=$this->post['admin_name'];
		$condition['admin_passwd']=$this->post['admin_passwd'];
		$condition['_logic'] = 'and';
		// 把查询条件传入查询方法
		$count=$Model->where($condition)->count();
		//echo $count;exit;
		if($count==0){
			return false;
		}
		return true;
	}

	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_user_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索 仅显示管理员组及以下
		$where='where a.admin_group_id>1 and a.admin_group_id=b.admin_group_id ';
		if($this->keyword){
			//$where.=" and (`username` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
			$where.=" and (`admin_name` LIKE '%".$this->keyword."%' )";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		//$list=$Model->query("select a.* from ".$this->table." ".$where." order by member_reg_time desc limit $start,$perpage");
		$sql="SELECT a.*,b.* FROM `think_admin` as a ,`think_admin_group` as b ".$where." order by a.add_time desc limit $start,$perpage";
		//echo $sql;exit;
		$list=$Model->query($sql);
		//print_r($list);exit;
		/*$Company=M('Company');
		//print_r($list);exit;
		foreach ($list as $k=>$v){
			$list[$k]['company_name']=$Company->where('member_id='.$v['member_id'])->getField('company_name');
		}*/
		//echo md5('bianji66');exit;
		//print_r($list);exit;
		return $list;
	}

	//获得总数
	public function get_user_total()
	{
		//搜索
		$where='where a.admin_group_id>1 and a.admin_group_id=b.admin_group_id ';
		if($this->keyword){
			//$where.="and a.member_id=b.member_id and (`member_email` LIKE '%".$this->keyword."%' or `member_mobile` LIKE '%".$this->keyword."%' or `member_truename` LIKE '%".$this->keyword."%' or `company_name` LIKE '%".$this->keyword."%')";
			$where.=" and (`admin_name` LIKE '%".$this->keyword."%' )";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(a.admin_id) as total from ".$this->table." as a, think_admin_group as b ".$where." ");
		return $query[0]['total'];
	}

	//获取信息
	public function get_admin_info()
	{
		$admin_id=intval($this->admin_id);
		$Model=M("Admin");
		$info=$Model->where('admin_id='.$admin_id)->find();
		return $info;
	}

	//编辑提交
	public function user_edit_post()
	{
		$Model=M("Admin");
		$condition['admin_id'] = $this->post['admin_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}

	//添加提交
	public function user_add_post()
	{
		$Model=M("Admin");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}

	//获取管理用户列表
	public function get_admin_list()
	{
		$where='where a.is_enable=1 and a.admin_group_id=4 and a.admin_group_id=b.admin_group_id ';
		$Model=new Model();
		//$list=$Model->query("select a.* from ".$this->table." ".$where." order by member_reg_time desc limit $start,$perpage");
		$sql="SELECT a.admin_id,a.admin_truename,b.admin_group_name FROM ".$this->table." as a ,`think_admin_group` as b ".$where." order by a.add_time desc ";
		//echo $sql;exit;
		$list=$Model->query($sql);
		//print_r($list);exit;
		/*$Company=M('Company');
		//print_r($list);exit;
		foreach ($list as $k=>$v){
			$list[$k]['company_name']=$Company->where('member_id='.$v['member_id'])->getField('company_name');
		}*/
		//echo md5('bianji66');exit;
		//dump($list);exit;
		return $list;
	}

}